package com.szqhyxj.youxiangju.biz.receipt.receiptdetail;

import com.szqhyxj.youxiangju.BaseConsumer;
import com.szqhyxj.youxiangju.sys.BaseResult;

import java.math.BigDecimal;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/11/23.
 */

public class ReceiptDetailPresenter implements ReceiptDetailContract.Presenter {
    ReceiptDetailContract.View view;

    String  transNo;
    public ReceiptDetailPresenter(ReceiptDetailContract.View view) {
        this.view = view;
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void init() {

    }

    @Override
    public void setAmount(String amount, String money,String tradeType,String minAmount,String maxAmount) {
        if (tradeType.equals("1")){
            if (amount == null || amount.equals("")) {
                view.showHandingFee("0.00" + "元");
                return;
            }
            if (Double.parseDouble(amount) > Double.parseDouble(money)) {
                view.showEnClickable();
                double fee = Double.parseDouble(amount) * 0.0079 + 1.0;
                BigDecimal b = new BigDecimal(fee);
                double fee1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                view.showHandingFee(String.format("%.2f", fee1) + "元");
                return;
            } else {
                if (Double.parseDouble(amount) < 100 || Double.parseDouble(amount) > 30000 || amount.equals("")) {
                    view.showEnClickable();
                } else {
                    double fee = Double.parseDouble(amount) * 0.0079 + 1.0;
                    BigDecimal b = new BigDecimal(fee);
                    double fee1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    view.showHandingFee(String.format("%.2f", fee1) + "元");
                    view.showClickable();
                }
            }
        }if (tradeType.equals("2")){
            if (amount == null || amount.equals("")) {
                view.showHandingFee("0.00" + "元");
                return;
            }
            if (Double.parseDouble(amount) > Double.parseDouble(money)) {
                view.showEnClickable();
                double fee = Double.parseDouble(amount) * 0.0065 + 2;
                BigDecimal b = new BigDecimal(fee);
                double fee1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                view.showHandingFee(String.format("%.2f", fee1) + "元");
                return;
            } else {
                double min = Integer.parseInt(minAmount);
                double max = Integer.parseInt(maxAmount);
                if (Double.parseDouble(amount) < min || Double.parseDouble(amount) > max || amount.equals("")) {
                    view.showEnClickable();
                } else {
                    double fee = Double.parseDouble(amount) * 0.0065 + 2;
                    BigDecimal b = new BigDecimal(fee);
                    double fee1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                    view.showHandingFee(String.format("%.2f", fee1) + "元");
                    view.showClickable();
                }
            }
        }


    }



    /**
     * 线上收款验证码获取
     *
     * @param channelCode
     * @param tradeCardId
     * @param amt
     */
    @Override
    public void onOnlineGetSms(String channelCode, String tradeCardId, String amt) {
        view.showLoading();
        ReceiptDetailModel.onlineGetsms(channelCode, tradeCardId, amt, "-1")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<ReceiptDetailModel.OnlineSmsEntity>>() {
                    @Override
                    public void accept(BaseResult<ReceiptDetailModel.OnlineSmsEntity> onlineSmsEntityBaseResult) throws Exception {
                        view.dismissLoading();
                        if (onlineSmsEntityBaseResult.businessLogicSuccess()) {
                                transNo = onlineSmsEntityBaseResult.getData().getOrderNo();
                        } else {
                            view.showToast(onlineSmsEntityBaseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showToast(throwable.getMessage());
                        view.enshowDialog();
                    }
                });
    }

    @Override
    public void receiptConfirm(String channelCode, String tradeCardId, String settleCardId, String tradeNo, String confirmCode) {
        view.showLoading();
        ReceiptDetailModel.receiptConfirm(channelCode, tradeCardId, settleCardId, this.transNo, confirmCode)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<ReceiptDetailModel.ConfirmEntity>>() {
                    @Override
                    public void accept(BaseResult<ReceiptDetailModel.ConfirmEntity> confirmEntityBaseResult) throws Exception {
                        view.dismissLoading();
                        if (confirmEntityBaseResult.businessLogicSuccess()) {
                            view.onlineSuccess();
                        } else {
                            view.showToast(confirmEntityBaseResult.getMessage() + "");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showToast(throwable.getMessage());
                    }
                });
    }


}
